package com.zxy.ziems.server.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.ziems.server.constant.ResourceType;
import com.zxy.ziems.server.domain.Resource;
import com.zxy.ziems.server.dto.ResourceTree;
import com.zxy.ziems.server.mapper.ResourceMapper;
import com.zxy.ziems.server.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author missrain
 * @date 2021/8/30 3:41 下午
 * @Email:wangsiyu-hq@boe.com.cn
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {
    @Autowired
    private ResourceMapper resourceMapper;
    @Override
    public List<ResourceTree> queryResourceTreeByResourceIdList(List<String> resourceIdList) {
        //根据id查询资源数据
        QueryWrapper<Resource> query = new QueryWrapper<>();
        query.lambda().in(CollectionUtil.isNotEmpty(resourceIdList),Resource::getResourceId,resourceIdList)
                .orderByAsc(Resource::getSortNo);
        List<Resource> resourceList = resourceMapper.selectList(query);
        return buildResourceTree(resourceList);
    }

    private List<ResourceTree> buildResourceTree(List<Resource> list) {
        List<ResourceTree> resourceTreeList = new ArrayList<>();
        Map<String,ResourceTree> map = new HashMap<>();
        list.forEach(resource -> {
            ResourceTree resourceTreeNode = new ResourceTree(resource.getResourceId(),
                    resource.getResourceName(),
                    resource.getUrl(),resource.getParentResourceId(),
                    resource.getResourceType(),resource.getSortNo(),false);
            map.put(resource.getResourceId(),resourceTreeNode);
            //将没有父节点的菜单加入到树中
            if (StrUtil.isEmpty(resource.getParentResourceId())) {
                resourceTreeList.add(resourceTreeNode);
            }
        });
        //循环数据将有父节点的数据加入到树中
        list.stream().filter(r->StrUtil.isNotEmpty(r.getParentResourceId()))
                .forEach(r->{
                    ResourceTree node = map.get(r.getResourceId());
                    ResourceTree parent = map.get(r.getParentResourceId());
                    Optional.ofNullable(parent).ifPresent(n -> {
                        if (ResourceType.BUTTON_TYPE == node.getResourceType()) {
                            n.setButtonFlag(true);
                        }
                        n.getChildren().add(node);
                    });
                });
        //树排序
        for (ResourceTree resourceTree : resourceTreeList) {
            List<ResourceTree> children = resourceTree.getChildren();
            if (null == children || children.isEmpty()){
                continue;
            }
            children.sort((o1, o2) -> {
                if (ResourceType.BUTTON_TYPE.equals(o1.getResourceType())){
                    return 0;
                }
                if (null == o1.getSortNo()){
                    return -1;
                }
                if (null == o2.getSortNo()){
                    return 1;
                }
                return o1.getSortNo()-o2.getSortNo();
            });
        }
        return resourceTreeList;
    }
}
